﻿using LpbPrj.Client.Tools.Helper;
using System;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace LpbPrj.Client.Tools.Converter
{
    /// <summary>
    /// 全路径转BitmapImage
    /// </summary>
    public class Path2BitmapImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;

            if (value is string imagePath &&
                !string.IsNullOrEmpty(imagePath) && File.Exists(imagePath) &&
                (Path.GetExtension(imagePath).Equals(".bmp") || Path.GetExtension(imagePath).Equals(".jpeg") || Path.GetExtension(imagePath).Equals(".jpg") || Path.GetExtension(imagePath).Equals(".png")))
            {
                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.CacheOption = BitmapCacheOption.OnLoad;
                bitmap.StreamSource = new MemoryStream(File.ReadAllBytes(imagePath));
                bitmap.EndInit();
                bitmap.Freeze();
                return bitmap;
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 编辑Dto属性路径转BitmapImage
    /// </summary>
    public class Path2BitmapImageExConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;

            object output = null;

            //参数名称判断
            if (parameter == null) return output;
            var param = System.Convert.ToString(parameter);
            if (string.IsNullOrEmpty(param)) return output;

            //利用反射机制获取图像相对路径属性值
            var property = value.GetType().GetProperty(param);
            if (property == null) return output;
            var imagePath = (string)property.GetValue(value);
            if (string.IsNullOrEmpty(imagePath)) return output;

            //患者Id
            var propertyPatientId = value.GetType().GetProperty("PatientId");
            if (propertyPatientId == null) return output;
            var patientId = (long)propertyPatientId.GetValue(value);
            if (patientId <= 0) return output;

            //检查记录Id
            var propertyRecordId = value.GetType().GetProperty("RecordId");
            if (propertyRecordId == null) return output;
            var recordId = (long)propertyRecordId.GetValue(value);
            if (recordId <= 0) return output;


            //图像完整路径
            var fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Result", patientId.ToString(), recordId.ToString());
            var imageFullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Result", patientId.ToString(), recordId.ToString(), imagePath);
            output = ImageShareHelper.File2BitmapImage(imageFullPath);
            return output;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 编辑Dto属性路径转全路径
    /// </summary>
    public class EditDtoFullPathConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;

            object output = null;

            //参数名称判断
            if (parameter == null) return output;
            var param = System.Convert.ToString(parameter);
            if (string.IsNullOrEmpty(param)) return output;

            //利用反射机制获取图像相对路径属性值
            var property = value.GetType().GetProperty(param);
            if (property == null) return output;
            var imagePath = (string)property.GetValue(value);
            if (string.IsNullOrEmpty(imagePath)) return output;

            //患者Id
            var propertyPatientId = value.GetType().GetProperty("PatientId");
            if (propertyPatientId == null) return output;
            var patientId = (long)propertyPatientId.GetValue(value);
            if (patientId <= 0) return output;

            //检查记录Id
            var propertyRecordId = value.GetType().GetProperty("RecordId");
            if (propertyRecordId == null) return output;
            var recordId = (long)propertyRecordId.GetValue(value);
            if (recordId <= 0) return output;


            //图像完整路径
            var imageFullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Result", patientId.ToString(), recordId.ToString(), imagePath);
            if (!string.IsNullOrEmpty(imageFullPath) && File.Exists(imageFullPath))
            {
                output = imageFullPath;
            }
            return output;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
